package cn.edu.cug.cs.gtl.docsrv.shp.updater;

import cn.edu.cug.cs.gtl.docsrv.shp.block.ShapeBlock;
import cn.edu.cug.cs.gtl.mybatis.Session;
import cn.edu.cug.cs.gtl.mybatis.mapper.shp.*;

import java.math.BigDecimal;

public interface BlockUpdater {
    /**
     * 将一个ShapeBlock插入数据库
     * @param block 图形块
     * @return 成功返回true,否则返回false
     */
    boolean insert(ShapeBlock block);

    /**
     * 读取本地目录中的所有图形文档，将其插入图形库中
     * @param localDir 本地目录
     * @return 成功返回true,否则返回false
     */
    boolean insert(String localDir);

    /**
     * 根据shpInternalId删除文档记录
     * @param block 必须填充shpInternalId
     * @return 成功返回true,否则返回false
     */
    boolean delete(ShapeBlock block);

    /**
     * 根据shpInternalId删除文档记录
     * @param shpInternalId
     * @return 成功返回true,否则返回false
     */
    boolean delete(BigDecimal shpInternalId);

    /**
     * 根据shpInternalId更新数据库中的记录
     * @param block 该对象的shpInternalId必须填充
     * @return 更新成功返回true,否则返回false
     */
    boolean update(ShapeBlock block);


    /**
     * 将一个图形文档插入数据库
     * SHPITEM为SHP_ITEM表中的一条记录
     * SHPFILE为SHP_FILE表中的一条记录
     * 通过SHP_INTERNAL_ID连接
     * @param shpItem 图形元数据信息
     * @param shpFile 图形文件内容
     * @return
     */
    static boolean insert(Session session, ShpItem shpItem, ShpFile shpFile){
        ShpItemMapper shpItemMapper = session.getMapper(ShpItemMapper.class);
        if(shpItem.getShpInternalId() == null){
            shpItem.setShpInternalId(shpItemMapper.nextInternalId());
        }
        shpItemMapper.insert(shpItem);
        ShpFileMapper shpFileMapper = session.getMapper(ShpFileMapper.class);
        shpFileMapper.insert(shpFile);
        session.commit();
        return true;
    }

    /**
     * 将shpAll插入图形库,
     * SHPALL为SHP_ALL数据表中的一条记录
     * 具体文件内容以BLOB方式存放
     * @param shpAll 图形文档
     * @return 成功返回true,否则返回false
     */
    static boolean insert(Session session, ShpAll shpAll){
        ShpAllMapper shpAllMapper = session.getMapper(ShpAllMapper.class);
        if(shpAll.getShpInternalId()==null) {
            shpAll.setShpInternalId(shpAllMapper.nextInternalId());
        }
        shpAllMapper.insert(shpAll);
        session.commit();
        return true;
    }

    /**
     * 根据shpInternalId删除SHP_ALL数据表中的图形文档记录
     * @param shpAll 必须填充shpInternalId
     * @return
     */
    static boolean delete(Session session,ShpAll shpAll){
        BigDecimal shpInternalId = shpAll.getShpInternalId();
        ShpAllMapper shpAllMapper = session.getMapper(ShpAllMapper.class);
        shpAllMapper.deleteByPrimaryKey(shpInternalId);
        session.commit();
        return true;
    }

    /**
     * 根据shpInternalId删除SHP_ITEM表中的一条记录和SHP_FILE表中的一条记录
     * @param shpItem 必须填充shpInternalId
     * @return
     */
    static boolean delete(Session session,ShpItem shpItem){
        BigDecimal shpInternalId = shpItem.getShpInternalId();
        ShpItemMapper shpItemMapper = session.getMapper(ShpItemMapper.class);
        shpItemMapper.deleteByPrimaryKey(shpInternalId);
        ShpFileMapper shpFileMapper = session.getMapper(ShpFileMapper.class);
        shpFileMapper.deleteByPrimaryKey(shpInternalId);
        session.commit();
        return true;
    }

    /**
     * 根据shpInternalId更新数据库中SHP_ITEM和SHP_FILE数据表的记录
     * @param shpItem 该对象的shpInternalId必须填充
     * @param shpFile 更新文档文件的内容
     * @return 更新成功返回true,否则返回false
     */
    static boolean update(Session session,ShpItem shpItem, ShpFile shpFile){
        ShpItemMapper shpItemMapper = session.getMapper(ShpItemMapper.class);
        shpItemMapper.updateByPrimaryKey(shpItem);
        ShpFileMapper docFileMapper = session.getMapper(ShpFileMapper.class);
        docFileMapper.updateByPrimaryKey(shpFile);
        session.commit();
        return true;
    }

    /**
     * 根据shpInternalId更新数据库中SHP_ALL数据表的记录
     * @param shpAll 该对象的shpInternalId必须填充
     * @return 更新成功返回true,否则返回false
     */
    static boolean update(Session session,ShpAll shpAll){
        ShpAllMapper shpAllMapper = session.getMapper(ShpAllMapper.class);
        shpAllMapper.updateByPrimaryKey(shpAll);
        session.commit();
        return true;
    }
}
